/**
 * Carnegie Mellon University
 * School of Computer Science
 * MSIT eBusiness Technology
 * 
 * Team 8 - Infinity (2011 Spring)
 * infinity-team8@googlegroups.com
 * 
 * Professor: Jeffrey L. Eppinger
 * Program Faculty: Sujata Telang
 */

package controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.EmployeeDAO;
import model.Model;
import model.CustomerDAO;
import model.TransactionDAO;

import org.mybeans.dao.DAOException;
import org.mybeans.form.FormBeanException;
import org.mybeans.form.FormBeanFactory;

import databean.CustomerBean;
import databean.EmployeeBean;
import databean.TransactionBean;
import formbean.EmployeeDepositCheckForm;


public class EmployeeDepositCheckAction extends Action {
	private FormBeanFactory<EmployeeDepositCheckForm> formBeanFactory = FormBeanFactory.getInstance(EmployeeDepositCheckForm.class);

	private CustomerDAO customerDAO;
	private EmployeeDAO employeeDAO;
	private TransactionDAO transactionDAO;
	
	public EmployeeDepositCheckAction(Model model) {
		customerDAO = model.getCustomerDAO();
		employeeDAO = model.getEmployeeDAO();
		transactionDAO = model.getTransactionDAO();
	}

	public String getName() { return "employee-deposit-check.do"; }

    public String perform(HttpServletRequest request,HttpServletResponse response) {
        List<String> errors = new ArrayList<String>();
        request.setAttribute("errors",errors);
        

        try {
        	

	        request.setAttribute("customerList",customerDAO.getCustomers());
	        request.setAttribute("employeeList",employeeDAO.getEmployees());
			request.setAttribute("employee",(EmployeeBean) request.getSession(false).getAttribute("employee"));
        	
			EmployeeDepositCheckForm form = formBeanFactory.create(request);
	        request.setAttribute("form",form);
	        
	        
	        request.setAttribute("customerList",customerDAO.getCustomers());
	        request.setAttribute("employeeList",employeeDAO.getEmployees());
	        
	        // If no params were passed, return with no errors so that the form will be
	        // presented (we assume for the first time).
	        if (!form.isPresent()) {
	            return "employee-deposit-check.jsp";
	        }
	
	        // Any validation errors?
	        errors.addAll(form.getValidationErrors());
	        if (errors.size() != 0) {
	            return "employee-deposit-check.jsp";
	        }
	
	        
	        CustomerBean customer = customerDAO.lookup(form.getUserName());
	        	        
	        // Create "Deposit" transaction			
			TransactionBean transaction = new TransactionBean(0);
			transaction.setCustomerID(customer.getCustomerID());
			transaction.setTransactionType("deposit");
			transaction.setAmount((long) Math.round(Double.parseDouble(form.getAmount())*100));
			transactionDAO.create(transaction);
			
			
			request.setAttribute("message","Customer account " + customer.getUserName() + " has been scheduled to be deposited on the next transition day.");
	        return "employee-success.jsp";
	        
        } catch (DAOException e) {
        	errors.add(e.getMessage());
        	return "employee-deposit-check.jsp";
        } catch (FormBeanException e) {
        	errors.add(e.getMessage());
        	return "employee-deposit-check.jsp";
        } catch (NumberFormatException e) {
        	errors.add(e.getMessage());
        	return "employee-deposit-check.jsp";
        }
    }
}
